package com.niit.project.dao.impl;

import com.niit.project.dao.GoodsDao;
import com.niit.project.entity.Goods;
import com.niit.project.entity.Page;
import com.niit.project.utils.Dbutils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Goods> selectAll(Page page) {
        try {
            List<Goods> goods = queryRunner.query(Dbutils.getConnection(),"select * from goods limit ?,?",new BeanListHandler<Goods>(Goods.class),page.getStartRows(),page.getPageSize());
            return goods;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        try {
            long count = (long) queryRunner.query(Dbutils.getConnection(),"select count(*) from goods",new ScalarHandler());
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(String goodname) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"delete from goods where goodname= ?",goodname);
            queryRunner.update(Dbutils.getConnection(),"ALTER TABLE goods AUTO_INCREMENT = 1;");
//            queryRunner.update(Dbutils.getConnection(),"alter table goods change column id id INT NOT NULL ;");
//            queryRunner.update(Dbutils.getConnection(),"delete from goods where id = ?",id);
//            queryRunner.update(Dbutils.getConnection(),"alter table goods change column id id INT NOT NULL auto_increment = ?;",id+1);
//            queryRunner.update(Dbutils.getConnection(),"alter table goods auto_increment = ?",id+1);
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Goods goods) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"insert into goods (goodname,goodtype,changemedium,changeway) values(?,?,?,?)",goods.getGoodname(),goods.getGoodtype(),goods.getChangemedium(),goods.getChangeway());
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Goods select(int id) {
        try {
            Goods goods = queryRunner.query(Dbutils.getConnection(),"select * from goods where id = ?",new BeanHandler<Goods>(Goods.class),id);
            return goods;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Goods goods) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"update goods set username = ?,goodname = ?, goodtype = ? ,changemedium = ?, changeway = ? where id = ?",goods.getUsername(),goods.getGoodname(),goods.getGoodtype(),goods.getChangemedium(),goods.getChangeway());
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goods> searchSelect(String option) {
//        try {
//            List<Goods> goods = queryRunner.query(Dbutils.getConnection(),"select * from goods where ? = ? limit ?,?",new BeanListHandler<Goods>(Goods.class),page.getStartRows(),page.getPageSize());
//            return goods;
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
        return null;
    }
}
